/**
 * 
 */
package com.py.credit.clean.code;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.utils.DateUtil;

/**
 * 时间比较规则验证
 * 
 * startTimeColumn should before endTimeColumn ,startTimeColumn || endTimeColumn
 * 为空可以通过
 * 
 * @author dd
 *
 */
public class TableDateOrderCheck {

	public boolean run(Map<String, Object> map, String extend) {

		boolean check = true;

		if (StringUtils.isNotBlank(extend)) {
			try {

				List<Map<String, String>> list = JSON.parseObject(extend,
						new TypeReference<List<Map<String, String>>>() {
						});

				if (list != null) {

					for (Map<String, String> parseObject : list) {

						String startDate = parseObject.get("startDate");

						String endDate = parseObject.get("endDate");

						if ("sysDate".equals(endDate)) {

							if (map.get(startDate) != null && !StringUtils.isBlank((String) map.get(startDate))) {
								String dateStr = (String) map.get(startDate);
								dateStr = dateStr.replace("-", "/");
								Date start = DateUtil.getInstance().strToDate(dateStr, DateUtil.YYYY__MM__DD);
								if (start.after(new Date())) {

									check = false;

									return check;
								}

							}

						} else if ("sysDate".equals(startDate)) {
							if (map.get(endDate) != null && !StringUtils.isBlank((String) map.get(endDate))) {
								String dateStr = (String) map.get(endDate);
								dateStr = dateStr.replace("-", "/");
								Date end = DateUtil.getInstance().strToDate(dateStr, DateUtil.YYYY__MM__DD);
								if (new Date().after(end)) {

									check = false;
									return check;
								}

							}

						} else {

							if (map.get(startDate) != null && !StringUtils.isBlank((String) map.get(startDate))
									&& map.get(endDate) != null && !StringUtils.isBlank((String) map.get(endDate))) {
								String startDateStr = (String) map.get(startDate);
								startDateStr = startDateStr.replace("-", "/");
								String endDateStr = (String) map.get(endDate);
								endDateStr = endDateStr.replace("-", "/");
								Date start = DateUtil.getInstance().strToDate(startDateStr, DateUtil.YYYY__MM__DD);
								Date end = DateUtil.getInstance().strToDate(endDateStr, DateUtil.YYYY__MM__DD);
								if (start.after(end)) {

									check = false;
									return check;
								}
							}

						}

					}

				}

			} catch (Exception e) {

				ExceptionLogger.error(e);

			}

		}

		return check;

	}

}
